<?php

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use kartik\depdrop\DepDrop;
use yii\widgets\LinkPager;
use app\modules\Product\models\Product;
use app\modules\Order\models\Order;
use app\models\Supplier;
use yii\widgets\Pjax;

$data = [];
$orders = $model->order;
if($orders ){

	$data = \yii\helpers\ArrayHelper::map($orders, 'order_id', 'order_id');
}
// 获取所有游轮公司
$_data = Product::find()->where(['status'=>1])->all();
$supplier = yii\helpers\ArrayHelper::map($_data, 'cruise_id', function($model){ return $model->cruise->name; } );

?>
<?PHP Pjax::begin(['id'=>'cruise', 'timeout'=>3000, 'linkSelector'=>'#cruise a[data-sort], #cruise a[data-page]']);?>
<style type="text/css">
	.item-name{display: inline-block;width: 70%;}
	.item-order {/*width:70px;*/width:20%;margin-left:15px;float:right;border:0px;border-bottom:1px solid #666;}
	.highlight {color: red;}
</style>

<?= yii\web\View::registerCssFile("/css/magic-check.css",['depends' => 'app\assets\AppAsset']) ?>

<h2 class="page-header"><small>绑定订单</small></h2>
<div class="finance-form">

		<div class="col-md-8">
			<div class="finance-search page-header" style="margin-top:0px;">
    			<?php $form = ActiveForm::begin([
    			    //'action' => ['index'],
        			'options' => ['data-pjax'=>true],
    			    'method' => 'get', 'layout' => 'inline',
    			]); ?>
    				<?= $form->field($productModel, 'cruise_id')->dropDownList($supplier, ['prompt'=>'选择游轮']) ?>
    				<?= $form->field($orderModel, 'code')->textInput(['placeholder' => Yii::t('app.order', 'Code')]) ?>
    				<?= $form->field($orderModel, 'username')->textInput(['placeholder' => Yii::t('app.order', 'Username')]) ?>
    			    <?= Html::submitButton(Yii::t('app', 'Search'), ['class' => 'btn btn-primary']) ?>
    			<?php ActiveForm::end(); ?>
    		</div>

			<ul class="list-group">
			<?php foreach($product as $val){ ?>
				<li class="list-group-item">
					<span class="badge"><?= $val->go_time?></span>
					<a data-toggle="collapse" href="#collapseExample<?= $val->id?>" aria-expanded="false" aria-controls="collapseExample" >
						<?= $val->name ?>
					</a>

					<div class="collapse" id="collapseExample<?= $val->id?>">
						<div style="margin:30px 0 0 20px;border-left:1px dotted #ccc;padding-left:15px;">
							<?php 
								//if($val->payOrderAll){
								if($val->newPayOrderAll){
									  foreach($val->newPayOrderAll as $order){
							?>
								<p>
								<?php $p1 = $order->price - $order->financeAllPrice; 
									  $p1 = ($p1< 0) ? 0 : $p1;
								?>
                					<input class="magic-checkbox magic" <?= in_array($order->id, array_keys($data)) ? 'checked' : '' ?>  type="checkbox" id="order_<?= $order->id?>" name="<?= $order->id?>" data-id=<?= $order->id ?> data-value="<?= $order->code ?> / <?= $order->username ?>" data-price="<?= $p1 ?>" >
									<label for="order_<?= $order->id?>" >
										<font class="<?= ($order->code == $orderModel->code) ? 'highlight' : '' ?>" ><?= $order->code ?> </font>
										&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
										<font class="<?= ($order->username == $orderModel->username) ? 'highlight' : '' ?>" style="width:150px;" ><?= $order->username ?></font> 
										&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
										<?= Yii::$app->formatter->asCurrency($order->price) ?>
										&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
										<?= Yii::$app->params['order_status'][$order->status]?> /
											<!--?= Yii::$app->formatter->asCurrency($order->financeAllPrice) ? -->
										<?php if($order->financeTotalPriceAll){ ?>
										<small style="color:#CCC;"> 
											已付 : <?= Yii::$app->formatter->asCurrency($order->financeTotalPriceAll) ?>
										</small>
										<?php } ?>
										<?php if($order->financeMustPriceAll){ ?>
										<small style="color:#669000;"> 
											应收 : <?= Yii::$app->formatter->asCurrency($order->financeMustPriceAll) ?>
										</small>
										<?php } ?>
									</label>
								</p>
							<?php 
									  }
								}else{
								  	echo "暂无订单";
								}
							?>
				    	</div>
					</div>
				</li>
			<?php } ?>
			<?= LinkPager::widget(['pagination' => $pages]); ?>
			</ul>
		</div>

</div>


<?php app\components\JsBlock::begin() ?>
<script type="text/javascript">

	$(".magic").click(function(){
        var checked = $(this).is(':checked');   // 是否选中
		var id 	= $(this).data('id');
		var name= $(this).data('value');
		var price = $(this).data('price');

		if(checked){

			var html = '<li id="all-item" class="list-group-item already_'+id+'">';
			var html = html + '<span class="item-name">'+name+'</span>';
			var html = html + '<input type="text" class="item-order" id="price_'+id+'" name="price[]" placeholder="金额" value='+price+' required />';
			var html = html + '<input type="hidden" name="order_id[]" value='+id+' />';
			var html = html + '</li>';

			$("._area").append(html);

			var total = $("#total-price").text();

			//total = parseInt(total);
			//price = parseInt(price);
			total = parseFloat(parseFloat(total).toFixed(2));
			price = parseFloat(parseFloat(price).toFixed(2));
			total = total + price;
			total = total.toFixed(2);
			$("#total-price").text(total);

		}else{
			
			var item = "already_"+id;
			
			var total = $("#total-price").text();
			var p = $("#price_"+id).val();
			
			//total = parseInt(total);
			//p = parseInt(p);
			total = parseFloat(parseFloat(total).toFixed(2));
			p = parseFloat(parseFloat(p).toFixed(2));
			total = total - p;
			total = total.toFixed(2);
			$("#total-price").text(total);

			$("."+item).remove();  // 移除该项
		}
	})

	// 修改金额  todo 
	$("._area").delegate("input", "change", function(){
		var total = 0 ;

		var all = $(".item-order");
		
		$(".item-order").each(function(){
			//	console.log($(this).val());	
			var price = $(this).val();
			//price = parseInt(price);
			price = parseFloat(parseFloat(price).toFixed(2));
			

			total += price;
		});
		total = total.toFixed(2);
		$("#total-price").text(total);
	})
	
</script>
<?php app\components\JsBlock::end() ?>

<?php Pjax::end(); ?>

<?php $form = ActiveForm::begin(['action' => '/finance/finance/finance-order']); ?>
	<div class="col-md-4">
		<input type="hidden" name="finance_id" value="<?= $model->id ?>">
		<ul class="list-group _area">
			<?php 
				if($orders){
					foreach($orders as $item){
			?>
						<li class="list-group-item already_<?= $item->order_id ?>">
							<?= $item->order->code ?>&nbsp;&nbsp;
							<?= $item->order->username ?>&nbsp;&nbsp;
							<?= $item->order->price ?>
						</li>	
			<?php 
					}
				}
			?>
		</ul>	
	</div>

	<div class="col-md-4"> 总计 : <font id="total-price">0</font> 元</div>
	<div class="col-md-4"><?= Html::submitButton(Yii::t('app', '绑定提交'), ['class' => 'btn btn-danger']) ?></div>
<?php ActiveForm::end(); ?>
